using Godot;
using System;

namespace Box;
public partial class CodePropertyControl : HBoxContainer,IProperty
{
	static CodeEditWindow code_editor;
	static public CodeEditWindow CodeEditor {
		get {
			if(code_editor == null) {
				code_editor = GD.Load<PackedScene>("res://source/process_tree_editor/code_editor_window/CodeEditWindow.tscn").Instantiate<CodeEditWindow>();
			}
			return code_editor;
		}
	}

	Button open_editor_button;
	public object PValue {get;set;}

	public string PName {get;set;}

	Label label;

	public override string ToString() {
		return $"()=>{{ {PValue.ToString()} }}";
	}

	public override void _Ready()
	{
		label = GetNode<Label>("Label");
		label.Text = PName;
		if(PValue == null) {
			PValue = "";
		}
		open_editor_button = GetNode<Button>("Input");
		open_editor_button.ButtonDown += _onOpenCodeEditor;
	}

	public void _onOpenCodeEditor() {
		if(!CodeEditor.IsInsideTree()) {
			GetTree().CurrentScene.AddChild(CodeEditor);
		}
		CodeEditor.Open(new_code_text => {
			PValue = new_code_text;
		});

		CodeEditor.EditControl.Text = PValue.ToString();

		CodeEditor.Show();
	}
}
